package com.longrise.co.ui.login.fragment;

import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.ui.login.contract.LoginContract;
import com.longrise.co.ui.login.model.LoginBean;
import com.longrise.co.ui.login.model.LoginModel;
import com.longrise.co.ui.login.presenter.LoginPresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-6-21.
 */

public class LoginFragment extends BaseFragment<LoginPresenter, LoginModel> implements View.OnClickListener, LoginContract.View<LoginBean> {

    @Bind(R.id.user)
    EditText user;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.login)
    Button login;
    @Bind(R.id.register)
    Button register;
    @Bind(R.id.eye)
    ImageView mEye;
    @Bind(R.id.forget_pwd)
    TextView forgetPwd;
    @Bind(R.id.head_title)
    TextView headTitle;

    private boolean mIsShowPwd = false;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_login;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        headTitle.setText("登录");
        regEvent(true);
        testInit();
        change2PwdDisplay(false);
    }

    private void testInit() {
//        user.setText("zheng1");
//        password.setText("12345a");
    }

    private void change2PwdDisplay(boolean isShowPwd) {
        this.mIsShowPwd = isShowPwd;
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        mEye.setBackgroundResource(resourceId);

        password.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void regEvent(boolean b) {

        if (login != null) {
            login.setOnClickListener(b ? this : null);
        }

        if (register != null) {
            register.setOnClickListener(b ? this : null);
        }

        if (mEye != null) {
            mEye.setOnClickListener(b ? this : null);
        }

        if (forgetPwd != null) {
            forgetPwd.setOnClickListener(b ? this : null);
        }
    }

    public static LoginFragment newInstance() {
        return new LoginFragment();
    }

    @Override
    public void onClick(View v) {
        if (checkDoubleClick()) {
            switch (v.getId()) {
                case R.id.login:

                    String userFlag = null;
                    String pwd = null;

                    if (user != null) {
                        userFlag = user.getText().toString().trim();
                    }

                    if (password != null) {
                        pwd = password.getText().toString().trim();
                    }
                    mPresenter.loginRequest(userFlag, pwd);
                    break;
                case R.id.register:
                    start(FastRegisterFragment.newInstance());
                    break;
                case R.id.eye:
                    change2PwdDisplay(!mIsShowPwd);
                    break;
                case R.id.forget_pwd:
                    start(ForgetPwdFragment.newInstance());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        regEvent(false);
    }


    @Override
    public void requestSuccess(LoginBean s) {
        //登录成功
        mRxManager.post(Constants.RX_EVENT_REFRESH_HOME, "loginSuccess");
        mRxManager.post(Constants.RX_EVENT_REFRESH_MINE, "loginSuccess");
        mRxManager.post(Constants.RX_EVENT_REFRESH_CAR_FILE,"loginSuccess");
        mRxManager.post(Constants.RX_EVENT_REFRESH_CAR_LIFE, "loginSuccess");
        mRxManager.post(Constants.RX_EVENT_QXDA, 0);
        _mActivity.onBackPressed();

    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }
}
